<?php
// 首页控制器
class Controller_WWW_Homepage extends Controller_WWW_Template
{
    public function __construct ($req, $res)
    {
        $this->template = "smarty:www/homepage";
        parent::__construct($req, $res);
    }

    public function action_index ()
    {
        // 哇吖动态
        $article = new Article;
        $this->template->set('articles', $article->listByType('哇吖动态', 0, 6));
        //玩法
        $line =new App_Line();
        $newlinelist=$line->get_track_and_act(null,0,10,'changed');
        $poplinelist=$line->get_track_and_act(null,0,10,'zan');
        $this->template->set('newlinelist',$newlinelist);
        $this->template->set('poplinelist',$poplinelist);
        // 热门活动
        $cates   = App_Category::all_categories(0, 100, FALSE, 3);
        $this->template->set('cates', $cates);
        //城市列表
        $line = new App_Line();
        $cityList=App_City::getCityListCn();
        $this->template->set('cityList',$cityList);
        
        $this->template->set('banners', App_Banner::items());
        $this->template->set('mbanners', App_Banner::centerBannerItems());

        // 日历今天
        $this->template->set('today', date("Y-m-d"));
    }

    public function action_search(){
        $query = $this->request->query();
//        var_dump($query);
        if(empty($query)){
            $this->request->redirect("/");
        }else{
            $city = Arr::get($query,'city');
            $start = Arr::get($query,'start');
            $end = Arr::get($query,'end');
            $counts = Arr::get($query,'counts');
            $activity = Arr::get($query,'activity');
            if($activity != ""){
//                $type = 'track';
                list($counts,$track_data) = App_Track_Search::getTrack($city,$start,$end,$counts);
                $this->template->set('track',$track_data);
                $this->template->set('counts',$counts);
//                var_dump($track_data);
                $this->template->set_filename('smarty:www/track');
            }else{
                $type = 'activity';
            }
        }
    }
     //邮箱注册成功
     public function action_regok ($url=NULL)
    {
        $query = $this->request->query();
        $url=Arr::get($query, 'url');
        switch ($url)
        {
           case 'qq':
           $url="https://mail.qq.com";
           break;
           case '163':
           $url="http://mail.163.com/";
           break;
           case 'waya':
           $url="http://qiye.163.com/login/";
           break;
           case 'sina':
           $url="http://mail.sina.com.cn/";
           break;
           case 'sohu':
           $url="http://mail.sohu.com/";
           break;
           case '126':
           $url="http://mail.126.com/";
           break;
           case 'hot':
           $url="http://www.hotmail.com/";
           break;
           case 'yahoo':
           $url="http://login.yahoo.com/";
           break;
           case 'gmail':
           $url="http://www.gmail.com/";
           break;
           case '189':
           $url="http://webmail30.189.cn/";
           break;
           case '139':
           $url="http://mail.10086.cn/";
           break;
           case 'tom':
           $url="http://web.mail.tom.com/";
           break;
           default:
           $url='';
        }
        
        $this->template->set_filename('smarty:www/regok');
        $this->template->set('email_url',$url);
    }
    // 404 页面
    public function action_not_found(){
        $this->template->set_filename('smarty:www/not_found');
    }

    //找回密码
    public function action_reset_pwd(){
        $post = $this->request->post();
        if(empty($post)){
            $this->template->set_filename('smarty:www/reset_pwd');
        }else{
            $email = Arr::get($post,'email');
            $data = User_Account::reset_pwd($email);
            $this->template->set('data',$data);
            $this->template->set_filename('smarty:www/subscribe');
//            $data = User_Account::reset_pwd($email);

        }
    }

    //验证用户
    public function action_passwd(){
        $query = $this->request->query();
        $token = Arr::get($query,'token');
        $data = User_Account::valid_token($token);
        if($data['status']){
            $this->template->set('data',$data);
            $this->template->set_filename('smarty:www/valid_succ');
        }else{
            $this->template->set('data',$data);
            $this->template->set_filename('smarty:www/valid_fail');
        }
    }

    public function action_confirm(){
        $query = $this->request->query();
        $token = Arr::get($query,'token');
        list($status,$msg) = User_Subscribe::confirm_token($token);
        if($status){
            $this->template->set('data',$msg['msg']);
            $this->template->set_filename('smarty:www/valid_succ_login');
        }else{
            $this->template->set('data',array('msg'=>$msg));
            $this->template->set_filename('smarty:www/valid_fail');
        }

    }

    public function action_tourist_phone_reg_confirm(){
    		$this->template->set('data','注册成功，请登录');
    		$this->template->set_filename('smarty:www/valid_succ_login');
    }

    //保存密码
    public function action_save_pwd(){
        $post = $this->request->post();
        $token = Arr::get($post,'token');
        $data = User_Account::valid_token($token);
        $ret = array();
        if($data['status']){
            $email = Arr::get($data,'email');
            $save['userToken'] = md5(Arr::get($post,'password'));
            User_Account::update($save,'userId',$email);
            $ret['status'] = 0;
            $ret['msg'] = '密码重置成功';
        }else{
            $ret['status'] = 0;
            $ret['msg'] = $data['msg'];
        }
        echo json_encode($ret);
        exit;
    }

    public function action_check_email($email){
        $query = User_Account::account_checkout('userId',$email);
        $ret = array('status'=>1,'msg'=>'用户不存在');
        if($query->count() > 0){
            $ret['status'] = 0;
            $ret['msg'] = '';
        }
        echo json_encode($ret);
//        var_dump($query);
        exit;
    }


    public function action_reset(){
        $post = $this->request->post();
        if(empty($post)){
            $this->template->set_filename('smarty:www/reset_pwd2');
        }else{
            $email = Arr::get($post,'email');
            $data = User_Account::reset_pwd($email);
            $this->template->set('data',$data);
            $this->template->set_filename('smarty:www/subscribe');
//            $data = User_Account::reset_pwd($email);

        }
    }


    public function action_check_user_email($email){
        $query = User_Account::account_checkout('userId',$email);
        $ret = array('status'=>1,'msg'=>'您的注册邮箱不匹配，请至个人中心认证中查看您的邮箱设置');
        if($query->count() > 0){
            $current = $query->current();
            if($this->_uid == $current['uid']){
                $ret['status'] = 0;
                $ret['msg'] = '';
            }

        }
        echo json_encode($ret);
//        var_dump($query);
        exit;
    }



}
